home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 4 / MacMania 4.toast / / Demo's / Igor Demo Pro / 1 PutContentsIn Igor Pro Folder / WaveMetrics Procedures / Utilities / Tutorial Engine < prev   
Text File  |  1994-02-18  |  7KB  |  302 lines

  1. | This file contains routines used by WaveMetrics to implement tutorial experiments.
  2. | See the “X Scaling Tutorial” in the “Learning Aids” folder for an example.
  3.  
  4. #include <Strings as Lists>
  5. #include <Keyword-Value>
  6.  
  7. Function TEInitGlobals()        | This is to auto-create and document globals used by the tutorial engine
  8.     Silent 1                                    | initializing . . .
  9.     Variable/G gTETutorialState = 0            | 0 = waiting for next step, 1 = waiting to execute commands
  10.     Variable/G gTETutorialParagraph = 0    | used to keep track of where in tutorial we are
  11.     String/G gTETutorialCmdText = ""        | commands waiting to be executed
  12.     String/G gTEControlCenter = "TE_ControlCenter"    | name of control center window
  13.     String/G gTETutorialData = "TE_TutorialData"        | name of notebook containing tutorial data
  14. End
  15.  
  16. | The client experiment must supply the following routines:
  17. |    TEClientStartOver
  18.  
  19. Proc TESetupTutorial()        | called when client experiment is loaded to do some initialization
  20.  
  21. End
  22.  
  23. Function/S TESplitParagraphUp(in, charsPerLine)
  24.     String in                            | input text
  25.     Variable charsPerLine                | desired approximate chars per line
  26.     
  27.     Variable totalLen
  28.     Variable i
  29.     Variable lineLen
  30.     String ch
  31.     String out = ""
  32.     
  33.     totalLen = strlen(in)
  34.     i = 0
  35.     lineLen = 0
  36.     do
  37.         ch = in[i] 
  38.         if ((lineLen >= charsPerLine) %& (cmpstr(ch, " ")==0))
  39.             out += "\r"
  40.             lineLen = 0
  41.         else
  42.             out += ch
  43.             lineLen += 1
  44.         endif
  45.         i += 1
  46.     while (i < totalLen)
  47.     
  48.     return out
  49. End
  50.  
  51. Function TESetHeading(text)
  52.     String text
  53.     
  54.     Textbox/N=heading/C "\Z12\f01" + text
  55. End
  56.  
  57. Function TESetMessage(text)
  58.     String text
  59.     
  60.     Textbox/N=message/C "\Z10" + text
  61. End
  62.  
  63. Function TESetCommand(text)
  64.     String text
  65.     
  66.     Textbox/N=command/C "\Z09\F'Monaco'" + text
  67. End
  68.  
  69. Function TESetTutorialState(state)
  70.     Variable state
  71.     
  72.     gTETutorialState = state
  73.     
  74.     if (state == 0)            | normal
  75.         TESetCommand("")
  76.         Button NextStep title="Next Step"
  77.     endif
  78.     
  79.     if (state == 1)            | waiting to execute a command
  80.         Button NextStep title="Execute"
  81.     endif
  82. End
  83.  
  84. Function TEExecuteCommandList(commands)
  85.     String commands
  86.     
  87.     String cmd
  88.     Variable i
  89.     
  90.     i = 0
  91.     do
  92.         cmd = GetStrFromList(commands, i, "\r")
  93.         if (strlen(cmd) == 0)
  94.             break
  95.         endif
  96.         Printf "•\t%s\r", cmd
  97.         Execute cmd
  98.         i += 1
  99.     while (1)
  100. End
  101.  
  102. Function TENextStep(ctrlName) : ButtonControl
  103.     String ctrlName
  104.     
  105.     Variable p
  106.     Variable isRegularMessage, isHiddenCommand, isImmediateCommand
  107.     Variable calledFromTEGoToSection
  108.     String message, command
  109.     String temp
  110.     
  111.     calledFromTEGoToSection = cmpstr(ctrlName, "TEGoToSection") == 0
  112.     
  113.     DoWindow/F $gTEControlCenter
  114.  
  115.     p = gTETutorialParagraph
  116.     
  117.     if (gTETutorialState == 1)        | waiting to execute a command ?
  118.         TEExecuteCommandList(gTETutorialCmdText)
  119.         gTETutorialCmdText = ""
  120.         TESetCommand("")
  121.         TESetMessage("")
  122.         TESetTutorialState(0)        | back to normal state
  123.     endif
  124.     
  125.     message = ""; command = ""
  126.     do
  127.         p += 1
  128.         Notebook $gTETutorialData selection={(p,0), (p,0)}, selection={startOfParagraph, endOfChars}
  129.         Notebook $gTETutorialData findText = {"", 1}        | for debugging, scroll selected text into view
  130.         
  131.         GetSelection notebook, $gTETutorialData, 3
  132.         if (V_startParagraph != p)
  133.             Beep; DoAlert 1, "This is the end of the tutorial. Do you want to start again?"
  134.             if (V_flag == 1)
  135.                 TEClientStartOver("TENextStep")    | Client must supply the TEClientStartOver routine
  136.                 p = gTETutorialParagraph
  137.             else
  138.                 p = V_startParagraph
  139.             endif
  140.             break
  141.         else
  142.             isRegularMessage = 1                                    | assume this is regular message
  143.             isImmediateCommand = 0
  144.             isHiddenCommand = 0
  145.             if (strlen(S_selection))
  146.                 if (cmpstr(S_selection[0], "*")==0)            | Is this a section heading?
  147.                     temp = S_selection[2, INF]                    | skip * and tab
  148.                     TESetHeading(temp)
  149.                     TESetMessage("")
  150.                     isRegularMessage = 0
  151.                 endif
  152.  
  153.                 if (cmpstr(S_selection[0], "•")==0)            | Is this an executable command?
  154.                     isRegularMessage = 0
  155.                     isImmediateCommand = cmpstr(S_selection[1, 1], "=")==0
  156.                     if (isImmediateCommand)
  157.                         Execute S_selection[2, INF]
  158.                     else
  159.                         isHiddenCommand = cmpstr(S_selection[1, 1], "•")==0
  160.                         if (strlen(gTETutorialCmdText))        | adding a new paragraph to command ?
  161.                             gTETutorialCmdText += "\r"
  162.                         endif
  163.                         if (!isHiddenCommand)
  164.                             if (strlen(command))                | adding a new paragraph to command ?
  165.                                 command += "\r"
  166.                             endif
  167.                         endif
  168.                         temp = S_selection[2, INF]                | skip • and tab
  169.                         gTETutorialCmdText += temp                | add to commands to be executed
  170.                         if (!isHiddenCommand)
  171.                             command += temp                        | add to commands to be displayed
  172.                         endif
  173.                     endif
  174.                 endif
  175.  
  176.                 if (cmpstr(S_selection[0], "-")==0)            | Is this the end of a step?
  177.                     if (!calledFromTEGoToSection)
  178.                         TESetMessage(message)
  179.                         TESetCommand(command)
  180.                     endif
  181.                     if (strlen(gTETutorialCmdText))
  182.                         TESetTutorialState(1)                    | waiting to execute a command
  183.                     else
  184.                         TESetTutorialState(0)                    | waiting for next step
  185.                     endif
  186.                     break
  187.                 endif
  188.                 
  189.                 if (isRegularMessage)
  190.                     if (strlen(message))        | adding a new paragraph to message ?
  191.                         message += "\r"
  192.                     endif
  193.                     temp = TESplitParagraphUp(S_selection, 90)
  194.                     message += temp
  195.                 endif
  196.             endif
  197.         endif
  198.     while (1)                        | continue till non-empty paragraph
  199.  
  200.     gTETutorialParagraph = p
  201. End
  202.  
  203. Function TEPrevStep(ctrlName) : ButtonControl
  204.     String ctrlName
  205.  
  206.     Variable p
  207.     Variable countDown = 2
  208.     
  209.     p = gTETutorialParagraph
  210.     do
  211.         p -= 1
  212.         if (p <= 0)
  213.             p = -1
  214.             break
  215.         endif
  216.         Notebook $gTETutorialData selection={(p, 0), (p, 2)}
  217.         GetSelection notebook, $gTETutorialData, 2
  218.         if (cmpstr(S_selection, "--") == 0)
  219.             countDown -= 1
  220.         endif
  221.     while (countDown)
  222.     gTETutorialParagraph = p
  223.     TESetTutorialState(0)                    | normal state
  224.     TENextStep("TEPrevStep")
  225. End
  226.  
  227. Function TEStartOver(nameOfCallingRoutine)
  228.     String nameOfCallingRoutine
  229.     
  230.     DoWindow/F $gTEControlCenter
  231.     gTETutorialParagraph = -1
  232.     gTETutorialCmdText = ""
  233.     TESetTutorialState(0)                | normal state
  234.     if (cmpstr(nameOfCallingRoutine, "TEGoToSection") != 0)
  235.         TENextStep("TEStartOver")
  236.     endif
  237. End
  238.  
  239. Function TEKillClientWindows(winTypesMask)
  240.     Variable winTypesMask
  241.     
  242.     String win
  243.     Variable index = 0
  244.     
  245.     do
  246.         win = WinName(index,winTypesMask)
  247.         if (strlen(win) == 0)
  248.             break
  249.         endif
  250.         if (CmpStr(win[0,2], "TE_") == 0)    | protected TE window ?
  251.             index += 1                            | skip this window
  252.         else
  253.             DoWindow/K $win
  254.         endif
  255.     while (1)
  256. End
  257.  
  258. Function TEGoToSection(section)
  259.     String section
  260.     
  261.     String temp
  262.     
  263.     TEClientStartOver("TEGoToSection")    | Client must supply the TEClientStartOver routine
  264.     do
  265.         TENextStep("TEGoToSection")
  266.         temp = StrByKey("TEXT", AnnotationInfo(gTEControlCenter, "heading"))
  267.         if (StrSearch(temp, section, 0) != -1)
  268.             break
  269.         endif
  270.     while (1)
  271. End
  272.  
  273. Function/S TESectionList()
  274.     String list
  275.     Variable pos1=0, pos2
  276.     
  277.     list = ""
  278.     Notebook $gTETutorialData selection={startOfFile, endOfFile}
  279.     GetSelection notebook, $gTETutorialData, 3        | all text is now in S_selection
  280.     do
  281.         pos1 = strsearch(S_selection, "*\t", pos1)
  282.         if (pos1 == -1)
  283.             break
  284.         endif
  285.         pos2 = strsearch(S_selection, "\r", pos1)
  286.         if (pos2 == -1)
  287.             break
  288.         endif
  289.         list += S_selection[pos1+2, pos2-1] + ";"
  290.         pos1 = pos2 + 1
  291.     while (1)
  292.     
  293.     return list
  294. End
  295.  
  296. Proc TEDoGoToSection(section)
  297.     String section
  298.     Prompt section, "Select section", popup TESectionList()
  299.     
  300.     TEGoToSection(section)
  301. End
  302.